package com.abe.libcore.view.common;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

public abstract class BaseJDialog extends Dialog {

    public BaseJDialog(@NonNull Context context) {
        super(context);
    }

    public BaseJDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BaseJDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    protected abstract int getLayoutRes();

    protected abstract void initContent();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(getLayoutRes());
        initContent();
    }

    @Override
    public void show() {
        super.show();
        Window dialogWindow = getWindow();
        if (dialogWindow == null) return;
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.85); // 宽度设置为屏幕的0.8
        dialogWindow.setAttributes(lp);
    }
}
